﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
//using Oracle.DataAccess.Client;
using System.Configuration;
//using System.Data.Entity.Infrastructure;
using System.ComponentModel.DataAnnotations;
using DslamPort.Models;
using System.Collections.ObjectModel;
using System.Web.Mvc;
using System.Data.OracleClient;
using System.IO;
using System.Data;

namespace DslamPort.Models
{
    public class DonviModel
    {
        public int quan_huyen_id { get; set; }
        public string ten_qh { get; set; }

        public int todanpho_id { get; set; }
        public string ten_tdp { get; set; }

        public int xa_phuong_id { get; set; }
        public string ten_xp { get; set; }
        public static List<DonviModel> getDonvi(string str="")
        {
            List<DonviModel> Donvi_ = new List<DonviModel>();
            var odr = BaseClass.Lib_GetDataReader("select * from quan_huyen");
            if (str == "")
            {
                foreach (DataRow item in odr.Rows)
                {
                    Donvi_.Add(new DonviModel
                    {
                        quan_huyen_id = Convert.ToInt32(item["quan_huyen_id"].ToString()),
                        ten_qh = item["ten"].ToString()
                    });
                }
            }
            else
            {
                foreach (DataRow item in odr.Rows)
                {
                    Donvi_.Add(new DonviModel
                    {
                        quan_huyen_id = Convert.ToInt32(item["quan_huyen_id"].ToString()),
                        ten_qh = item["ten"].ToString() + "_" + item["quan_huyen_id"].ToString()
                    });
                }
            }
            return Donvi_;
        }
        public List<DonviModel> getKhuvuc()
        {
            List<DonviModel> Donvi_ = new List<DonviModel>();
            var odr = BaseClass.Lib_GetDataReader("select * from xa_phuong");
            foreach (DataRow item in odr.Rows)
            {
                Donvi_.Add(new DonviModel
                {
                    xa_phuong_id = Convert.ToInt32(item["xa_phuong_id"].ToString()),
                    ten_xp = item["ten"].ToString()

                });
            }
            
            return Donvi_;
        }
        public List<DonviModel> getTodanpho()
        {
            List<DonviModel> Donvi_ = new List<DonviModel>();
            var odr = BaseClass.Lib_GetDataReader("select * from todanpho");
            foreach (DataRow item in odr.Rows)
            {
                Donvi_.Add(new DonviModel
                {
                    todanpho_id = Convert.ToInt32(item["todanpho_id"].ToString()),
                    ten_tdp = item["ten"].ToString()

                });
            }
            
            return Donvi_;
        }
        public List<DonviModel> getKhuvucByDonvi(string quan_huyen_id)
        {
            List<DonviModel> Donvi_ = new List<DonviModel>();
            string sql = "select * from xa_phuong where quan_huyen_id=" + quan_huyen_id;
            var odr = BaseClass.Lib_GetDataReader(sql);
            foreach (DataRow item in odr.Rows)
            {
                Donvi_.Add(new DonviModel
                {
                    xa_phuong_id = Convert.ToInt32(item["xa_phuong_id"].ToString()),
                    ten_xp = item["ten"].ToString()

                });
            }
            return Donvi_;
        }
        public List<DonviModel> getTodanphoByKhuvuc(string xa_phuong_id)
        {
            List<DonviModel> Donvi_ = new List<DonviModel>();
            string sql = "select * from todanpho where xa_phuong_id=" + xa_phuong_id + " and trim(ten) is not null order by regexp_substr(ten, '^\\D*') nulls first,to_number(regexp_substr(ten, '\\d+'))";
            var odr = BaseClass.Lib_GetDataReader(sql);
            foreach (DataRow item in odr.Rows)
            {
                Donvi_.Add(new DonviModel
                {
                    todanpho_id = Convert.ToInt32(item["todanpho_id"].ToString()),
                    ten_tdp = item["ten"].ToString()

                });
            }
           
            return Donvi_;
        }
    }
}
